package com.iotproject.studyservice.controller.admin;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.StudyGrade;
import com.iotproject.studyservice.service.StudyGradeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("admin/studyservice/studygrade/")
public class AdminStudyGradeController {

    @Autowired
    StudyGradeService studyGradeService;

    //获取单词分类列表
    @GetMapping("getWordGradeList")
    public R getWordGradeList() {
        List<StudyGrade> list = studyGradeService.list(null);
        return R.ok().data("gradeList", list);
    }

    //删除单词类别方法
    @DeleteMapping("deleteGrade/{id}")
    @ApiOperation(value = "根据id删除单词类别")
    public R deleteGradeById(@PathVariable("id") String id) {
        boolean result = studyGradeService.removeById(id);
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据id获取单词类别
    @GetMapping("getGradeMsg/{id}")
    public R getGradeMsgById(@PathVariable("id") String id) {
        StudyGrade grade = studyGradeService.getById(id);
        return R.ok().data("grade", grade);
    }

    //修改单词类别信息
    @PostMapping("updateGradeMsg")
    public R updateGradeMsg(@RequestBody StudyGrade studyGrade) {
        //studyGrade 包含id
        boolean updateGrade = studyGradeService.updateById(studyGrade);
        if (updateGrade) {
            return R.ok();
        }
        return R.error();
    }

    //传入Excel文件，将内容存储到数据库
    @PostMapping("addGrade")
    public R upload(MultipartFile file) {
        studyGradeService.importStudyGradeData(file, studyGradeService);
        return R.ok();
    }

}
